package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.bean.SceneBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * 规则，定时，场景所用到的选择控制设备列表界面的适配器
 * Created by David on 2015/8/17.
 */
public class DeviceActionsAdapter extends BaseAdapter {
    //
    private final int RULE = 2;
    private final int SCENE = 3;
    private final int UNKNOWN = -1;
    private final int SCHEDULE = 1;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private ArrayList<String> actions;
    private int type = -1;

    /**
     * 构造函数
     * @param context
     * @param actions 所包含的可以控制的设备
     * @param type    类型  排程还是规则还是场景
     */
    public DeviceActionsAdapter(Context context, ArrayList<String> actions, int type) {
        this.type = type;
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        this.actions = actions;
        if (this.actions == null)
            this.actions = new ArrayList<String>();
    }

    @Override
    public int getCount() {
        return actions.size();
    }

    @Override
    public Object getItem(int position) {
        return actions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.listview_device_list, null);
            holder.tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
            holder.ivDeviceIcon = (ImageView) convertView.findViewById(R.id.iv_device_icon);
            holder.rl_modify = (RelativeLayout) convertView.findViewById(R.id.rl_modify);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.rl_modify.setVisibility(View.GONE);
        String address = actions.get(position);
        if (type == SCHEDULE) {
            DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(address);
            if (deviceBean != null) {
                if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.ph_icon);
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.temperature_icon);
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.water_lever_icon);
                } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
                } else {
                    holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
                }
                holder.tvDeviceName.setText(deviceBean.getName());
                String tag = deviceBean.getUserTag();
                if (tag != null) {
                    if (DeviceConstant.nameMap.get(tag) == null) {
                        holder.tvDeviceName.setText(tag);
                    } else {
                        holder.tvDeviceName.setText(DeviceConstant.nameMap.get(tag));
                    }
                } else {
                    holder.tvDeviceName.setText(deviceBean.getName());
                }
            } else {
                String sceneId = actions.get(position);
                SceneBean sceneBean = SceneBean.queryScene(sceneId);
                if (sceneBean != null) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.scene);
                    holder.tvDeviceName.setText(sceneBean.getTitle());
                }
            }
        } else {
            if (position == actions.size() - 1) {
                holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
                holder.tvDeviceName.setText(R.string.device_action_wait);
            } else {
                DeviceBean deviceBean = SystemConstant.deviceBeanHashMap.get(address);
                if (deviceBean != null) {
                    if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.ph_icon);
                    } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.temperature_icon);
                    } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
                        if (deviceBean.getUserTag() != null) {
                            if (deviceBean.getUserTag().equals("Light Switch")) {
                                holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                            } else if (deviceBean.getUserTag().equals("Feed Switch")) {
                                holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
                            } else if (deviceBean.getUserTag().equals("Water Switch")) {
                                holder.ivDeviceIcon.setImageResource(R.drawable.water_switch_icon);
                            } else if (deviceBean.getUserTag().equals("O2 Switch")) {
                                holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                            } else {
                                holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                            }
                        } else {
                            holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                        }
                    } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.water_lever_icon);
                    } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
                    } else {
                        holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
                    }
                    String tag = deviceBean.getUserTag();
                    if (tag != null) {
                        holder.tvDeviceName.setText(DeviceConstant.nameMap.get(tag));
                    } else {
                        holder.tvDeviceName.setText(deviceBean.getName());
                    }
                } else {
                    String sceneId = actions.get(position);
                    SceneBean sceneBean = SceneBean.queryScene(sceneId);
                    if (sceneBean != null) {
                        holder.ivDeviceIcon.setImageResource(R.drawable.scene);
                        holder.tvDeviceName.setText(sceneBean.getTitle());
                    }
                }
            }
        }
        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tvDeviceName;
        ImageView ivDeviceIcon;
        RelativeLayout rl_modify;
    }
}
